<?php
/**
 * Created by PhpStorm.
 * User: fusha_000
 * Date: 10-14/0014
 * Time: 17:48
 */

class Sync {

    private $db;
    private $platform;
    private $col_suffix;

    /**
     * 同步成功
     */
    const SYNC_OK = 1;
    /**
     * 同步失败
     */
    const SYNC_FAIL = 2;



    public function __construct($db, $platform)
    {
        $this->db = $db;
        $this->platform = $platform;
        if ($platform=='PFT') {
            $this->col_suffix = 'pft';
        }
        elseif($platform=='16U') {
            $this->col_suffix = 'xxw';
        }


    }

    /**
     * 获取先行同步数据
     *
     * @param $db
     * @param $lid
     * @return mixed
     */
    public static function getSyncLand($db, $lid)
    {
        //`delaytime`
        $sql = <<<SQL
SELECT `title`, `tel`, `fax`, `jtype`, `opentime`,`runtime`, `area`, `address`,
`jtzn`,`jdjj`, `bhjq`, `jqts`, `imgpath`,`status`, `px`, `addtime`, `passtime`,
`topic`, `uptime`, `p_type`, `taobao_support`,`attribute`
FROM uu_land WHERE id=$lid LIMIT 1
SQL;
        $db->query($sql);
        return $db->fetch_assoc();
    }

    /**
     * 获取平台本地数据
     *
     * @param $le
     * @param $lid
     * @return mixed
     */
    public static function getLocalLand($le, $lid,$col_suffix)
    {
        $sql = <<<SQL
SELECT `title`, `tel`, `fax`, `jtype`, `opentime`,`runtime`, `area`, `address`,
`jtzn`,`jdjj`, `bhjq`, `jqts`, `imgpath`,`status`, `px`, `addtime`, `passtime`,
`topic`, `uptime`, `p_type`, `taobao_support`,`apply_did` as apply_did_{$col_suffix},
`attribute`, `terminal`, `delaytime`
FROM uu_land WHERE id=$lid LIMIT 1
SQL;
        $le->query($sql);
        return $le->fetch_assoc();
    }

    public static function getSyncTicket($le, $id, $col_suff='', $where='')
    {
        $sql = <<<SQL
        SELECT `title`,`landid`,`apply_did`, `tprice`, `reb`, `rebp`, `discount`, `delaydays`,
        `uniont`, `mobre`, `px`, `status`, `pay`, `notes`, `ifs`, `storage`,
        `storage_open`, `ddays`, `getaddr`, `smslimit`, `s_limit_up`,
        `s_limit_low`, `buy_limit_up`, `buy_limit_low`, `open_time`,
        `end_time`, `modify_op`, `uuid`, `pid`, `order_limit`, `order_end`,
        `delaytype`,`sync_id_{$col_suff}` as pft_rflex_id,`origin`
        FROM sync_uu_ticket WHERE id=$id
SQL;
        if (!empty($where)) {
            $sql .= " AND " . $where;
        }
        $le->query($sql);
        return $le->fetch_assoc();
    }

    public static function getLocalTicket($le, $tid)
    {
        $sql = <<<SQL
 SELECT `landid` , `title`, `tprice`, `reb`, `rebp`,
 `discount`, `delaydays`,`uniont`,`mobre`, `px`, `status`, `pay`, `notes`, `ifs`,
 `storage`,`storage_open`,`ddays`, `getaddr`, `smslimit`, `s_limit_up`,
 `s_limit_low`,`buy_limit_up`,`buy_limit_low`, `open_time`, `end_time`,
 `apply_did` as apply_did,`modify_op`, `uuid`,
 `pid`, `order_limit`, `order_end`, `delaytype`
FROM uu_jq_ticket WHERE id={$tid}
SQL;
        $le->query($sql);
        return $le->fetch_assoc();
    }

    public static function getLocalLandF($le, $tid)
    {
        $sql = <<<SQL
    SELECT `lid`, `tid`, `pid`, `p_type`, `dhour`, `confirm_sms`,
    `manual_verify`, `mdays`, `mhour`, `g_fund`, `startplace`,
    `endplace`, `rdays`, `tourist_info`, `ass_station`, `series_model`,
    `v_mobile_support`, `v_ID_support`, `v_time_limit` FROM uu_land_f
    WHERE tid=$tid;
SQL;
        $le->query($sql);
        return $le->fetch_assoc();

    }

    public static function getSyncLandF($le, $id)
    {
        $sql = <<<SQL
    SELECT `lid`, `tid`, `pid`, `p_type`, `dhour`, `confirm_sms`,
    `manual_verify`, `mdays`, `mhour`, `g_fund`, `startplace`,
    `endplace`, `rdays`, `tourist_info`, `ass_station`, `series_model`,
    `v_mobile_support`, `v_ID_support`, `v_time_limit` FROM sync_uu_land_f
    WHERE id=$id;
SQL;
        $le->query($sql);
        return $le->fetch_assoc();
    }

    public static function SyncTickets($le, Array $tickets)
    {
        if (!is_array($tickets) || ! count($tickets)) {
            return array('code'=>101,'msg'=>'门票数据不存在');
        }
        $sql = sqlInsert('uu_jq_ticket', $tickets);
        $le->query($sql);
    }


    /**
     * 变更同步状态
     *
     * @param $le
     * @param $id
     * @param int $status 1成功，2失败
     * @param string $otr_suffix 另一个平台前缀
     * @param int $sync_id 另一个平台主键ID
     * @param string $tbl
     */
    public static function ChgSyncStatus($le, $id, $status=1,$sync_id=0)
    {
        $sql = "UPDATE sync_data SET if_sync={$status},sync_time=now() WHERE id=$id";
        return $le->query($sql);
    }

    static public function writeLog($word, $filepath) {
        $maxsize  =  10485760;//1024*1024*10;
        if(file_exists($filepath)){
            $filesize = filesize($filepath);
            $filetype = end(explode(".",$filepath)); //获取文件后缀名
            $filename = substr($filepath,0,strpos($filepath,'.'));
            if($filesize>$maxsize) {
                rename($filepath, $filename.'_'.date('Ymd').'.'.$filetype);
            }
        }
        $fp = fopen($filepath,"a");
        flock($fp, LOCK_EX) ;
        fwrite($fp,"\n".$word);
        flock($fp, LOCK_UN);
        fclose($fp);
    }
    static function Notify($data) {
        $url = REQUEST_URL . 'module/sync/handler.php';
        request_post($url, $data);
    }

    static function GetSyncLog($le, $tbname, $col_id, $origin=1)
    {
        $sql = <<<SQL
SELECT id,col,chg_new FROM sync_data
WHERE tbname='$tbname' AND col_id=$col_id AND origin=$origin AND if_sync=0
SQL;
        $le->query($sql);
        $rows = array();
        while($row=$le->fetch_assoc()) {
            $rows[$row['id']] = $row;
        }
        return $rows;
    }
} 